﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jemgine.MapEditor
{
	public partial class EntityList : UserControl
	{
		public EntityList()
		{
			InitializeComponent();
		}

		EditorState EditorState;

		internal void SetEditorState(EditorState ES)
		{
			EditorState = ES;
			EditorState.Tree = this.TreeView.Nodes;
			EditorState.TreeChanged += this.Invalidate;
            EditorState.OnSelection += ItemSelected;
		}

        void ItemSelected()
        {
            try
            {
                if (EditorState.Selected.Type == Jemgine.Engine.Selection.Types.MapElement)
                    this.TreeView.SelectedNode = this.TreeView.Nodes[EditorState.Selected.TopIndex];
                else if (EditorState.Selected.Type == Jemgine.Engine.Selection.Types.EntityElement)
                    this.TreeView.SelectedNode = this.TreeView.Nodes[EditorState.Selected.TopIndex].Nodes[EditorState.Selected.MiddleIndex];
                else
                    this.TreeView.SelectedNode = null;
            }
            catch (Exception) { this.TreeView.SelectedNode = null; }

            this.Invalidate();
        }

		private void TreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
            Engine.Selection select = e.Node.Tag as Engine.Selection;
            EditorState.SelectItemEx(select);
		}

	}
}
